package com.nit.lab.mapper.basic;

import com.nit.lab.dto.basic.DeptDTO;
import com.nit.lab.entity.basic.DeptEntity;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @Description
 * @Author HWY
 * @Date 2020/12/14 10:50
 **/
public interface DeptMapper {

    /**
     * @Description: 获取单位表格数据
     * @Author HWY
     * @Date 2020/12/14 10:50
     */
    @Select({"<script>SELECT " +
            "dept_id AS `key`, dept_id, " +
            "dept_name, dept_info, " +
            "dept_father, dept_type, gmt_create " +
            "FROM basic_dept " +
            "WHERE TRUE " +
            "<if test='deptName != null'> AND dept_name LIKE CONCAT('%', #{deptName}, '%') </if>" +
            "ORDER BY dept_id ASC " +
            "LIMIT #{start}, #{length}" +
            "</script>"})
    List<DeptDTO> list4Table(
            @Param("start") Integer start,
            @Param("length") Integer length,
            @Param("deptName") String deptName) throws Exception;

    /**
     * @Description: 获取单位表格数据总条数
     * @Author HWY
     * @Date 2020/12/14 10:50
     */
    @Select({"<script>SELECT COUNT(*) " +
            "FROM basic_dept " +
            "WHERE TRUE " +
            "<if test='deptName != null'> AND dept_name LIKE CONCAT('%', #{deptName}, '%') </if>" +
            "</script>"})
    Long count4Table(
            @Param("deptName") String deptName) throws Exception;

    /**
     * @Description: 新增单位信息
     * @Author HWY
     * @Date 2020/12/14 10:50
     */
    @Insert("INSERT INTO basic_dept (" +
            "dept_name, " +
            "dept_info, dept_father, " +
            "dept_type, gmt_create) " +
            "VALUES (#{deptEntity.deptName}, " +
            "#{deptEntity.deptInfo}, #{deptEntity.deptFather}, " +
            "#{deptEntity.deptType},  #{deptEntity.gmtCreate})")
    @Options(useGeneratedKeys = true, keyProperty = "deptId", keyColumn = "dept_id")
    Integer add(@Param("deptEntity") DeptEntity deptEntity) throws Exception;

    /**
     * @Description: 更新单位信息
     * @Author HWY
     * @Date 2020/12/14 10:50
     */
    @Update("UPDATE basic_dept " +
            "SET dept_name = #{deptEntity.deptName}, " +
            "dept_info = #{deptEntity.deptInfo}, " +
            "dept_father = #{deptEntity.deptFather}, " +
            "dept_type = #{deptEntity.deptType} " +
            "WHERE dept_id = #{deptEntity.deptId}")
    Integer update(@Param("deptEntity") DeptEntity deptEntity) throws Exception;

    /**
     * @Description: 删除单位信息
     * @Author HWY
     * @Date 2020/12/14 10:50
     */
    @Delete("DELETE FROM basic_dept WHERE dept_id = #{deptId}")
    Integer remove(@Param("deptId") Long deptId) throws Exception;

    /**
     * @Description: 获取单位信息用于选择
     * @Author: HWY
     * @Date: 2020/12/14 22:45
     */
    @Select("SELECT dept_id, dept_name, dept_info, " +
            "dept_father, dept_type, gmt_create " +
            "FROM basic_dept ")
    List<DeptDTO> listDept() throws Exception;

    /**
     * @Description: 获取单位信息树
     * @Author HWY
     * @Date 2020/12/20
     */
    @Select("SELECT dept_id, dept_name, dept_father, dept_type FROM basic_dept ")
    @Results({
            @Result(column = "dept_id", property = "deptId"),
            @Result(column = "dept_name", property = "deptName"),
            @Result(column = "dept_father", property = "deptFathers"),
            @Result(column = "dept_type", property = "deptTypes")
    })
    List<DeptDTO> listDeptTree() throws Exception;
}
